package com.awanp3.nimbus.common.json.handler;

import com.awanp3.nimbus.common.core.enums.FormatsType;
import com.awanp3.nimbus.common.json.context.TimeZoneContextHolder;
import com.fasterxml.jackson.core.JsonGenerator;
import com.fasterxml.jackson.databind.JsonSerializer;
import com.fasterxml.jackson.databind.SerializerProvider;
import lombok.extern.slf4j.Slf4j;

import java.io.IOException;
import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.TimeZone;

/**
 * 自定义 Date 类型序列化处理器（支持多种格式）
 *
 * @author 20014170
 */
@Slf4j
public class CustomDateSerializer extends JsonSerializer<Date> {

    @Override
    public void serialize(Date date, JsonGenerator jsonGenerator, SerializerProvider serializerProvider) throws IOException {

        TimeZone timeZone = TimeZoneContextHolder.getTimeZone();
        DateFormat format = new SimpleDateFormat(FormatsType.YYYY_MM_DD_HH_MM_SS.getTimeFormat());
        format.setTimeZone(timeZone);
        jsonGenerator.writeString(format.format(date));

    }

}
